﻿using LineFocus.Nikcron.Models;
using Nickron.Database;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using LineFocus.Nikcron.Common;

namespace LineFocus.Nikcron.Controllers
{
    public class MasterController : BaseController
    {
        public ActionResult Zone()
        {
            return View();
        }

        public ContentResult AddEditLookup(String LookupType, Int32? Id, String Name)
        {
            ApplicationDBContext db = new ApplicationDBContext();
            if (Id.HasValue)
            {
                switch (LookupType)
                { 
                    case "Zone":
                        Zone Zone = db.Zones.Where(z => z.Id == Id.Value).FirstOrDefault();
                        Zone.Id = Id.Value;
                        Zone.Name = Name;
                        db.SaveChanges();
                        break;
                    case "ProductType":
                        ProductType ProductType = db.ProductTypes.Where(z => z.Id == Id.Value).FirstOrDefault();
                        ProductType.Id = Id.Value;
                        ProductType.Name = Name;
                        db.SaveChanges();
                        break;

                    case "ServiceType":
                        ServiceType ServiceType = db.ServiceTypes.Where(z => z.Id == Id.Value).FirstOrDefault();
                        ServiceType.Id = Id.Value;
                        ServiceType.Name = Name;
                        db.SaveChanges();
                        break;

                    case "ReturnType":
                        ReturnType ReturnType = db.ReturnTypes.Where(z => z.Id == Id.Value).FirstOrDefault();
                        ReturnType.Id = Id.Value;
                        ReturnType.Name = Name;
                        db.SaveChanges();
                        break;
                }
                
            }
            else
            {
                switch (LookupType)
                {
                    case "Zone":
                        Zone Zone = new Zone();
                        Zone.Name = Name;
                        db.Zones.AddObject(Zone);
                        db.SaveChanges();
                        break;
                    case "ProductType":
                        ProductType ProductType = new ProductType();
                        ProductType.Name = Name;
                        db.ProductTypes.AddObject(ProductType);
                        db.SaveChanges();
                        break;
                    case "ServiceType":
                        ServiceType ServiceType = new ServiceType();
                        ServiceType.Name = Name;
                        db.ServiceTypes.AddObject(ServiceType);
                        db.SaveChanges();
                        break;
                    case "ReturnType":
                        ReturnType ReturnType = new ReturnType();
                        ReturnType.Name = Name;
                        db.ReturnTypes.AddObject(ReturnType);
                        db.SaveChanges();
                        break;
                }
                
            }
            ContentResult Result = new ContentResult();
            Result.Content = "Success";
            return Result;
        }

        public ActionResult LookupList(String LookupType)
        {
            ApplicationDBContext db = new ApplicationDBContext();
            Lookup Lookup = new Lookup();
            switch (LookupType)
            {
                case "Zone":
                    var query1 = from records in db.Zones
                                select new LookupItem() { Id = records.Id, Name = records.Name };
                    Lookup.Items = query1.ToList();
                    break;
                case "Company":
                    var query2 = from records in db.Offices.OfType<Company>()
                                select new LookupItem() { Id = records.Id, Name = records.Name };
                    Lookup.Items = query2.ToList();
                    break;
                case "ProductType":
                    var query3 = from records in db.ProductTypes
                                select new LookupItem() { Id = records.Id, Name = records.Name };
                    Lookup.Items = query3.ToList();
                    break;
                case "ServiceType":
                    var query4 = from records in db.ServiceTypes
                                select new LookupItem() { Id = records.Id, Name = records.Name };
                    Lookup.Items = query4.ToList();
                    break;
                case "ReturnType":
                    var query5 = from records in db.ReturnTypes
                                select new LookupItem() { Id = records.Id, Name = records.Name };
                    Lookup.Items = query5.ToList();
                    break;
                case "Manufacture":
                    var query6 = from records in db.Manufactures
                                select new LookupItem() { Id = records.Id, Name = records.Name };
                    Lookup.Items = query6.ToList();
                    break;
                case "Stockhouse":
                    var query7 = from records in db.Offices.OfType<Stockhouse>()
                                select new LookupItem() { Id = records.Id, Name = records.Name };
                    Lookup.Items = query7.ToList();
                    break;
            }
            return View(Lookup);
        }

        public JsonResult GetLookups(String LookupType)
        {
            ApplicationDBContext db = new ApplicationDBContext();
            JsonResult JsonData = new JsonResult();
            switch (LookupType)
            { 
                case "Zone":
                    JsonData.Data = db.Zones.Select(n => new { n.Id, n.Name }).ToList();
                    break;
                case "Company":
                    JsonData.Data = db.Offices.OfType<Company>().Select(n => new { n.Id, n.Name }).ToList();
                    break;
                case "ProductType":
                    JsonData.Data = db.ProductTypes.Select(n => new { n.Id, n.Name }).ToList();
                    break;
                case "ServiceType":
                    JsonData.Data = db.ServiceTypes.Select(n => new { n.Id, n.Name }).ToList();
                    break;
                case "ReturnType":
                    JsonData.Data = db.ReturnTypes.Select(n => new { n.Id, n.Name }).ToList();
                    break;
                case "Manufacture":
                    JsonData.Data = db.Manufactures.Select(n => new { n.Id, n.Name }).ToList();
                    break;
                case "Stockhouse":
                    JsonData.Data = db.Offices.OfType<Stockhouse>().Select(n => new { n.Id, n.Name }).ToList();
                    break;
            }
            JsonData.JsonRequestBehavior = JsonRequestBehavior.AllowGet;
            return JsonData;
        }

        public JsonResult GetCities(String Name)
        {
            ApplicationDBContext db = new ApplicationDBContext();
            JsonResult Result = new JsonResult();
            Result.Data = db.Cities.Where(c => c.Name.StartsWith(Name)).Take(20).ToList().Select(n => new { n.Id, n.Name });
            Result.JsonRequestBehavior = JsonRequestBehavior.AllowGet;
            return Result;
        }

        public JsonResult GetStateCountry(Int32 CityId)
        {
            ApplicationDBContext db = new ApplicationDBContext();
            JsonResult Result = new JsonResult();
            var data = from s in db.States
                       join c in db.Countries on s.CountryId equals c.Id
                       join ct in db.Cities on s.Id equals ct.StateId
                       where ct.Id == CityId
                       select new
                       {
                           CountryId = c.Id,
                           CountryName = c.Name,
                           StateId = s.Id,
                           StateName = s.Name
                       };
            Result.Data = data;
            return Result;
        }

        public JsonResult GetStates(String Country)
        {
            ApplicationDBContext db = new ApplicationDBContext();
            JsonResult Result = new JsonResult();
            var data = from s in db.States
                       join c in db.Countries on s.CountryId equals c.Id
                       where c.Name == Country
                       select new
                       {
                           s.Id,
                           s.Name
                       };
            Result.Data = data;
            Result.JsonRequestBehavior = JsonRequestBehavior.AllowGet;
            return Result;
        }

        public JsonResult GetCountries()
        {
            ApplicationDBContext db = new ApplicationDBContext();
            var data = db.Countries.Select(n => new { n.Id, n.Name });
            return Json(data, JsonRequestBehavior.AllowGet);
        }

        public JsonResult GetStockhouse(Int32 Zoneid)
        {
            ApplicationDBContext db = new ApplicationDBContext();
            JsonResult Result = new JsonResult();
            var data = from o in db.Offices
                       join s in db.Offices.OfType<Stockhouse>() on o.Id equals s.Id
                       select new
                       {
                           id = o.Id,
                           Name = o.Name
                       };
            Result.Data = data;
            return Result;
        }


        #region ServiceType Maintanance

        public ActionResult ServiceTypes()
        {
            ViewBag.Header = "ServiceType";
            ViewBag.Caption = "Listing";
            ViewBag.Module = "Master";
            ApplicationDBContext db = new ApplicationDBContext();
            ServiceType ServiceType = new ServiceType();
            return View(ServiceType);
        }

        public JsonResult GetServiceTypes()
        {
            ApplicationDBContext db = new ApplicationDBContext();
            var ServiceTypes = from c in db.ServiceTypes.OfType<ServiceType>().ToList().Where(c => c.Status.IsDeleted != true)
                               select new
                               {
                                   Id = c.Id,
                                   Name = c.Name,
                               };
            JsonResult result = new JsonResult();
            result.Data = ServiceTypes;
            result.JsonRequestBehavior = JsonRequestBehavior.AllowGet;
            return result;
        }

        [HttpGet]
        public ActionResult ServiceTypeMaintenance(Int32? Id)
        {
            ViewBag.Header = "ServiceType";
            if (Id.HasValue)
            {
                ViewBag.Caption = "Edit";
                ViewBag.Id = Id;
                ApplicationDBContext db = new ApplicationDBContext();
                ServiceType ServiceType = db.ServiceTypes.OfType<ServiceType>().Where(z => z.Id == Id).ToList().FirstOrDefault();
                return View(ServiceType);
            }
            else
            {
                ViewBag.Caption = "New";
                ViewBag.Id = "0";
                return View(new ServiceType());
            }
        }

        [HttpPost]
        public ActionResult ServiceTypeMaintenance(FormCollection formCollection)
        {
            ApplicationDBContext db = new ApplicationDBContext();
            ServiceType ServiceType = new ServiceType();

            Int32 Id = 0;
            Int32.TryParse(formCollection["Id"], out Id);
            if (Id == 0)
                ServiceType = new ServiceType();
            else
                ServiceType = db.ServiceTypes.OfType<ServiceType>().Where(z => z.Id == Id).FirstOrDefault();

            ServiceType.Name = formCollection["ServiceType"];

            if (Id == 0)
            {
                db.ServiceTypes.AddObject(ServiceType);

                TempData["AlertMessage"] = "Data has been Added succeessfully";
            }
            else
            {
                TempData["AlertMessage"] = "Data has been Updated Successfully";
            }
            db.SaveChanges();

            return RedirectToAction("ServiceTypes");
        }

        public ActionResult DeleteServiceType(Int32? Id)
        {
            ViewBag.Header = "ServiceType";
            if (Id.HasValue)
            {
                ViewBag.Caption = "DeleteServiceType";
                ViewBag.Id = Id;
                ApplicationDBContext db = new ApplicationDBContext();
                ServiceType ServiceType = db.ServiceTypes.OfType<ServiceType>().Where(z => z.Id == Id).FirstOrDefault();
                db.DeleteObject(ServiceType);
                db.SaveChanges();
            }

            TempData["AlertMessage"] = "Data has been deleted succeessfully";
            return RedirectToAction("ServiceTypes");
        }


        #endregion

        #region ProductTypes Maintenane

        public ActionResult ProductTypes()
        {
            ViewBag.Header = "ProductType";
            ViewBag.Caption = "Listing";
            ViewBag.Module = "Master";
            ApplicationDBContext db = new ApplicationDBContext();
            ProductType ProductType = new ProductType();
            return View(ProductType);
        }

        public JsonResult GetProductTypes()
        {
            ApplicationDBContext db = new ApplicationDBContext();
            var ProductTypes = from p in db.ProductTypes.OfType<ProductType>().ToList().Where(p => p.Status.IsDeleted != true)
                        select new
                        {
                            Id = p.Id,
                            Name = p.Name,
                        };
            JsonResult result = new JsonResult();
            result.Data = ProductTypes;
            result.JsonRequestBehavior = JsonRequestBehavior.AllowGet;
            return result;
        }

        [HttpGet]
        public ActionResult ProductTypesMaintenance(Int32? Id)
        {
            ViewBag.Header = "ProductType";
            if (Id.HasValue)
            {
                ViewBag.Caption = "Edit";
                ViewBag.Id = Id;
                ApplicationDBContext db = new ApplicationDBContext();
                ProductType ProductType = db.ProductTypes.OfType<ProductType>().Where(p => p.Id == Id).ToList().FirstOrDefault();
                return View(ProductType);
            }
            else
            {
                ViewBag.Caption = "New";
                ViewBag.Id = "0";
                return View(new ProductType());
            }
        }    

        [HttpPost]
        public ActionResult ProductTypesMaintenance(FormCollection formCollection)
        {
            ApplicationDBContext db = new ApplicationDBContext();
            ProductType ProductType = new ProductType();

            Int32 Id = 0;
            Int32.TryParse(formCollection["Id"], out Id);
            if (Id == 0)
                ProductType = new ProductType();
            else
                ProductType = db.ProductTypes.OfType<ProductType>().Where(p => p.Id == Id).FirstOrDefault();

            ProductType.Name = formCollection["ProductType"];

            if (Id == 0)
            {
                db.ProductTypes.AddObject(ProductType);

                TempData["AlertMessage"] = "Data has been Added successfully";
            }
            else
            {
                TempData["AlertMessage"] = "Data has been Updated Successfully";
            }
            db.SaveChanges();


            return RedirectToAction("ProductTypes");
        }

        public ActionResult DeleteProductType(Int32? Id)
        {
            ViewBag.Header = "ProductType";
            if (Id.HasValue)
            {
                ViewBag.Caption = "Delete";
                ViewBag.Id = Id;
                ApplicationDBContext db = new ApplicationDBContext();
                ProductType ProductType = db.ProductTypes.OfType<ProductType>().Where(p => p.Id == Id).FirstOrDefault();
                db.DeleteObject(ProductType);
                db.SaveChanges();
            }

            TempData["AlertMessage"] = "Data has been deleted succeessfully";
            return RedirectToAction("ProductTypes");
        }

        #endregion

        #region Brand or Manufacture

        public ActionResult Brands()
        {
            ViewBag.Header = "Brand";
            ViewBag.Caption = "Listing";
            ViewBag.Module = "Master";
            ApplicationDBContext db = new ApplicationDBContext();
            Manufacture Manufacture = new Manufacture();
            return View(Manufacture);
        }
        public JsonResult GetBrands()
        {
            ApplicationDBContext db = new ApplicationDBContext();
            var Manufactures = from c in db.Manufactures.OfType<Manufacture>().ToList().Where(c => c.Status.IsDeleted != true)
                               select new
                               {
                                   Id = c.Id,
                                   Name = c.Name,
                               };
            JsonResult result = new JsonResult();
            result.Data = Manufactures;
            result.JsonRequestBehavior = JsonRequestBehavior.AllowGet;
            return result;
        }

        [HttpGet]
        public ActionResult BrandMaintenance(Int32? Id)
        {
            ViewBag.Header = "Brand";
            if (Id.HasValue)
            {
                ViewBag.Caption = "Edit";
                ViewBag.Id = Id;
                ApplicationDBContext db = new ApplicationDBContext();
                Manufacture Manufacture = db.Manufactures.OfType<Manufacture>().Where(z => z.Id == Id).ToList().FirstOrDefault();
                return View(Manufacture);
            }
            else
            {
                ViewBag.Caption = "New";
                ViewBag.Id = "0";
                return View(new Manufacture());
            }
        }

        [HttpPost]
        public ActionResult BrandMaintenance(FormCollection formCollection)
        {
            ApplicationDBContext db = new ApplicationDBContext();
            Manufacture Manufacture = new Manufacture();

            Int32 Id = 0;
            Int32.TryParse(formCollection["Id"], out Id);
            if (Id == 0)
                Manufacture = new Manufacture();
            else
                Manufacture = db.Manufactures.OfType<Manufacture>().Where(z => z.Id == Id).FirstOrDefault();

            Manufacture.Name = formCollection["Manufacture"];

            if (Id == 0)
            {
                db.Manufactures.AddObject(Manufacture);

                TempData["AlertMessage"] = "Data has been Added succeessfully";
            }
            else
            {
                TempData["AlertMessage"] = "Data has been Updated Successfully";
            }
            db.SaveChanges();

            return RedirectToAction("Brands");
        }

        public ActionResult DeleteBrand(Int32? Id)
        {
            ViewBag.Header = "Brand";
            if (Id.HasValue)
            {
                ViewBag.Caption = "DeleteManufacture";
                ViewBag.Id = Id;
                ApplicationDBContext db = new ApplicationDBContext();
                Manufacture Manufacture = db.Manufactures.OfType<Manufacture>().Where(z => z.Id == Id).FirstOrDefault();
                db.DeleteObject(Manufacture);
                db.SaveChanges();
            }

            TempData["AlertMessage"] = "Data has been deleted succeessfully";
            return RedirectToAction("Brands");
        }

        #endregion

        #region Zone Maintenane

        public ActionResult Zones()
        {
            ViewBag.Header = "Zone";
            ViewBag.Caption = "Listing";
            ViewBag.Module = "Master";
            ApplicationDBContext db = new ApplicationDBContext();
            Zone Zone = new Zone();
            return View(Zone);            
        }

        public JsonResult GetZones()
        {
            ApplicationDBContext db = new ApplicationDBContext();
            var Zones = from c in db.Zones.OfType<Zone>().ToList().Where(c => c.Status.IsDeleted != true)
                            select new
                            {
                                Id = c.Id,
                                Name = c.Name,                               
                            };
            JsonResult result = new JsonResult();
            result.Data = Zones;
            result.JsonRequestBehavior = JsonRequestBehavior.AllowGet;
            return result;
        }

        [HttpGet]
        public ActionResult ZoneMaintenance(Int32? Id)
        {
            ViewBag.Header = "Zone";
            if (Id.HasValue)
            {
                ViewBag.Caption = "Edit";
                ViewBag.Id = Id;
                ApplicationDBContext db = new ApplicationDBContext();
                Zone Zone = db.Zones.OfType<Zone>().Where(z => z.Id == Id).ToList().FirstOrDefault();
                return View(Zone);
            }
            else
            {
                ViewBag.Caption = "New";
                ViewBag.Id = "0";
                return View(new Zone());
            }
        }      

        [HttpPost]
        public ActionResult ZoneMaintenance(FormCollection formCollection)
        {
            ApplicationDBContext db = new ApplicationDBContext();
            Zone Zone = new Zone();

            Int32 Id = 0;
            Int32.TryParse(formCollection["Id"], out Id);
            if (Id == 0)
                Zone = new Zone();
            else
                Zone = db.Zones.OfType<Zone>().Where(z => z.Id == Id).FirstOrDefault();

            Zone.Name = formCollection["Zone"];

            if (Id == 0)
            {
                db.Zones.AddObject(Zone);

                TempData["AlertMessage"] = "Data has been Added successfully";
            }
            else
            {
                TempData["AlertMessage"] = "Data has been Updated Successfully";
            }
            db.SaveChanges();


            return RedirectToAction("Zones");
        }

        public ActionResult DeleteZone(Int32? Id)
        {
            ViewBag.Header = "Zone";
            if (Id.HasValue)
            {
                ViewBag.Caption = "Delete";
                ViewBag.Id = Id;
                ApplicationDBContext db = new ApplicationDBContext();
                Zone Zone = db.Zones.OfType<Zone>().Where(z => z.Id == Id).FirstOrDefault();
                db.DeleteObject(Zone);
                db.SaveChanges();
            }

            TempData["AlertMessage"] = "Data has been deleted successfully";
            return RedirectToAction("Zones");
        }

        #endregion

        #region Country Maintenance
        
        public ActionResult Countrys()
        {
            ViewBag.Header = "Country";
            ViewBag.Caption = "Listing";
            ViewBag.Module = "Master";
            ApplicationDBContext db = new ApplicationDBContext();
            Country Country = new Country();
            return View(Country); 
        }

        public JsonResult GetCountry()
        {
            ApplicationDBContext db = new ApplicationDBContext();
            var Countrys = from c in db.Countries.OfType<Country>().ToList().Where(c => c.Status.IsDeleted != true)
                        select new
                        {
                            Id = c.Id,
                            Name = c.Name,
                        };
            JsonResult result = new JsonResult();
            result.Data = Countrys;
            result.JsonRequestBehavior = JsonRequestBehavior.AllowGet;
            return result;
        }

        [HttpGet]
        public ActionResult CountryMaintenance(Int32? Id)
        {
            ViewBag.Header = "Country";
            if (Id.HasValue)
            {
                ViewBag.Caption = "Edit";
                ViewBag.Id = Id;
                ApplicationDBContext db = new ApplicationDBContext();
                Country Country = db.Countries.OfType<Country>().Where(c => c.Id == Id).ToList().FirstOrDefault();
                return View(Country);
            }
            else
            {
                ViewBag.Caption = "New";
                ViewBag.Id = "0";
                return View(new Country());
            }
        }

        [HttpPost]
        public ActionResult CountryMaintenance(FormCollection formCollection)
        {
            ApplicationDBContext db = new ApplicationDBContext();
            Country Country = new Country();

            Int32 Id = 0;
            Int32.TryParse(formCollection["Id"], out Id);
            if (Id == 0)
                Country = new Country();
            else
                Country = db.Countries.OfType<Country>().Where(c => c.Id == Id).FirstOrDefault();

            Country.Name = formCollection["Country"];

            if (Id == 0)
            {
                db.Countries.AddObject(Country);

                TempData["AlertMessage"] = "Data has been Added successfully";
            }
            else
            {
                TempData["AlertMessage"] = "Data has been Updated Successfully";
            }
            db.SaveChanges();


            return RedirectToAction("Countrys");
        }

        public ActionResult DeleteCountry(Int32? Id)
        {
            ViewBag.Header = "Country";
            if(Id.HasValue)
            {
                ViewBag.Caption = "Delete";
                ViewBag.Id = Id;
                ApplicationDBContext db = new ApplicationDBContext();
                Country Country = db.Countries.OfType<Country>().Where(c => c.Id == Id).FirstOrDefault();
                db.DeleteObject(Country);
                db.SaveChanges();
            }
            TempData["AlertMessage"] = "Data has been deleted succeessfully";
            return RedirectToAction("Countrys");            
        }

        #endregion

        #region State Maintenance

        public ActionResult States()
        {
            ViewBag.Header = "State";
            ViewBag.Caption = "Listing";
            ViewBag.Module = "Master";
            ApplicationDBContext db = new ApplicationDBContext();
            State State = new State();
            return View(State); 
        }

        public JsonResult GetState()
        {
            ApplicationDBContext db = new ApplicationDBContext();
            var States = from c in db.Countries.OfType<Country>().ToList()
                         join s in db.States.OfType<State>().ToList().Where(s => s.Status.IsDeleted != true) on c.Id equals s.CountryId
                         select new
                         {
                             CountryId=c.Id,
                             CountryName=c.Name,
                             Id = s.Id,
                             Name = s.Name
                         };
            JsonResult result = new JsonResult();
            result.Data = States;
            result.JsonRequestBehavior = JsonRequestBehavior.AllowGet;
            return result;           
        }

        [HttpGet]
        public ActionResult StateMaintenance(Int32? Id)
        {
            Lookup Lookup_Countries = new Lookup();

            ApplicationDBContext db = new ApplicationDBContext();

            var query1 = from records in db.Countries.OfType<Country>().ToList().Where(c => c.Status.IsDeleted != true)
                         select new LookupItem() { Id = records.Id, Name = records.Name };
            Lookup_Countries.Items = query1.ToList();
            
            ViewBag.Header = "State";
            ViewBag.Countries = Lookup_Countries;

            if (Id.HasValue)
            {
                ViewBag.Id = Id;
                ViewBag.Caption = "Edit";

                State State = db.States.OfType<State>().Where(s => s.Id == Id).FirstOrDefault();
                return View(State);
            }
            else
            {                
                State State = new State();                
                ViewBag.Caption = "New";                
                return View(State);
            }
        }

        [HttpPost]
        public ActionResult StateMaintenance(FormCollection formCollection)
        {
            ApplicationDBContext db = new ApplicationDBContext();

            State State;
            Int32 StateId = 0;
            Int32.TryParse(formCollection["Id"], out StateId);
            if (StateId == 0)
            {
                State = new State();
                string sss = formCollection["Country"];
                State.CountryId = Convert.ToInt16(sss);  
            }
            else
                State = db.States.OfType<State>().Where(s => s.Id == StateId).FirstOrDefault();
          
            State.Name = formCollection["State"];

            if (StateId == 0)
                db.States.AddObject(State);
            db.SaveChanges();

            return RedirectToAction("States");
        }

        public ActionResult DeleteState(Int32? Id)
        {
            ViewBag.Header = "State";
            if (Id.HasValue)
            {
                ViewBag.Caption = "Delete";
                ViewBag.Id = Id;
                ApplicationDBContext db = new ApplicationDBContext();
                State State = db.States.OfType<State>().Where(c => c.Id == Id).FirstOrDefault();
                db.DeleteObject(State);
                db.SaveChanges();
            }
            TempData["AlertMessage"] = "Data has been deleted succeessfully";
            return RedirectToAction("States");
        }

        #endregion

        #region City Maintenance

        public ActionResult Cities()
        {
            ViewBag.Header = "City";
            ViewBag.Caption = "Listing";
            ViewBag.Module = "Master";
            ApplicationDBContext db = new ApplicationDBContext();
            City City = new City();
            return View(City);
        }

        [HttpGet]
        public JsonResult GetCity()
        {
            ApplicationDBContext db = new ApplicationDBContext();
            var Cities = from c in db.Countries.OfType<Country>().ToList()
                         join s in db.States.OfType<State>().ToList() on c.Id equals s.CountryId
                         join ci in db.Cities.OfType<City>().ToList().Where(ci => ci.Status.IsDeleted != true) on s.Id equals ci.StateId
                         select new
                         {
                             CountryId = c.Id,
                             CountryName = c.Name,
                             StateId = s.Id,
                             StateName = s.Name,
                             Id = ci.Id,
                             Name = ci.Name
                         };
            JsonResult result = new JsonResult();
            result.Data = Cities;
            result.JsonRequestBehavior = JsonRequestBehavior.AllowGet;
            return result;
        }

        [HttpGet]
        public ActionResult CityMaintenance(Int32? Id)
        {          
            Lookup Lookup_States = new Lookup();
            Lookup Lookup_Countries = new Lookup();          

            ApplicationDBContext db = new ApplicationDBContext();

            var query1 = from records in db.Countries.OfType<Country>().ToList().Where(c => c.Status.IsDeleted != true)
                         select new LookupItem() { Id = records.Id, Name = records.Name };
            Lookup_Countries.Items = query1.ToList();

            var query2 = from records in db.States.OfType<State>()
                         select new LookupItem() { Id = records.Id, Name = records.Name, ParentId = records.CountryId };
            Lookup_States.Parent = "Country";
            Lookup_States.Items = query2.ToList();

            ViewBag.Header = "City";         
            ViewBag.States = Lookup_States;
            ViewBag.Countries = Lookup_Countries;            

            if (Id.HasValue)
            {
                ViewBag.Id = Id;
                ViewBag.Caption = "Edit";
                City city = db.Cities.OfType<City>().Where(d => d.Id == Id).FirstOrDefault();
                         //   join s in db.States.OfType<State>().ToList() on c.Id equals s.CountryId;

                return View(city);
            }
            else
            {              
                ViewBag.Caption = "New";
                City city = new City();                 
                return View(city);
            }
        }

        [HttpPost]
        public ActionResult CityMaintenance(FormCollection formCollection)
        {
            ApplicationDBContext db = new ApplicationDBContext();
            City City;
            User User = new Nickron.Database.User();
            Int32 CityId = 0;
            Int32.TryParse(formCollection["Id"], out CityId);
            if (CityId == 0)
            {
                City = new City();
                City.StateId = Int32.Parse(formCollection["State"]);
            }
            else
                City = db.Cities.OfType<City>().Where(s => s.Id == CityId).FirstOrDefault();
            
            City.Name = formCollection["City"];

            if (CityId == 0)
                db.Cities.AddObject(City);
            db.SaveChanges();
         
            return RedirectToAction("Cities");
        }

        public ActionResult DeleteCity(Int32? Id)
        {
            ViewBag.Header = "City";
            if (Id.HasValue)
            {
                ViewBag.Caption = "Delete";
                ViewBag.Id = Id;
                ApplicationDBContext db = new ApplicationDBContext();
                City City = db.Cities.OfType<City>().Where(c => c.Id == Id).FirstOrDefault();
                db.DeleteObject(City);
                db.SaveChanges();
            }
            TempData["AlertMessage"] = "Data has been deleted succeessfully";
            return RedirectToAction("Cities");
        }

        #endregion
      
    }
}
